package com.excilys.patricksbank.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.excilys.patricksbank.dao.api.OperationDao;
import com.excilys.patricksbank.model.Compte;
import com.excilys.patricksbank.model.Operation;

@Repository
public class OperationDaoImpl implements OperationDao {

	@Resource
	private HibernateTemplate hibernateTemplate;

	@SuppressWarnings("unchecked")
	public List<Operation> getOperationParCompte(Compte c) {

		return hibernateTemplate.find("from Operation o where o.compte = ?", c);
	}

	public List<Operation> getOperationParCompteEtParDate(Compte c, int annee, int mois) {

		DateTime dateDeb = new DateTime(annee + "-" + (mois+1) + "-01");
		DateTime dateFin =  new DateTime(annee + "-" + (mois+2) + "-01");
		
		DetachedCriteria criteria = DetachedCriteria.forClass(Operation.class);
		
		criteria.add(Restrictions.between("date", dateDeb, dateFin));
		//criteria.add(Restrictions.ne("typeOperation", Operation.TypeOperation.PAIEMENT_CARTE));
		criteria.add(Restrictions.eq("compte", c));
		criteria.addOrder(Order.asc("date"));
		return (List<Operation>) hibernateTemplate.findByCriteria(criteria);
	}
}
